package com.leetcode.No1769;

import org.junit.Test;

import java.util.Arrays;

/**
 * @program: Solution
 * @description: 移动所有球到每个盒子所需的最小操作数
 * @author: Wang Zhihua
 * @date: 2022-12-05
 */
public class Solution {
    public int[] minOperations(String boxes) {
        int len = boxes.length();
        int[] boxArray = new int[len];
        for (int i = 0; i < len; ++i) {
            boxArray[i] = boxes.charAt(i) - '0';
        }

        int[] sum1 = new int[len];
        int[] step1 = new int[len];
        sum1[0] = boxArray[0];
        step1[0] = 0;
        for (int i = 1; i < len; ++i) {
            step1[i] = step1[i - 1] + sum1[i - 1];
            sum1[i] = sum1[i - 1] + boxArray[i];
        }

        int[] sum2 = new int[len];
        int[] step2 = new int[len];
        sum2[len - 1] = boxArray[len - 1];
        step2[len - 1] = 0;
        for (int i = len - 2; i >= 0; --i) {
            step2[i] = step2[i + 1] + sum2[i + 1];
            sum2[i] = sum2[i + 1] + boxArray[i];
        }

        int[] answer = new int[len];
        for (int i = 0; i < len; ++i) {
            answer[i] = step1[i] + step2[i];
        }
        return answer;
    }

    @Test
    public void test01() {
        String boxes;

        boxes = "110";
        System.out.println(Arrays.toString(minOperations(boxes)));
    }
}
